package com.gongjian.web.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.gongjian.database.entity.Classe;
import com.gongjian.database.entity.User;
import com.gongjian.database.service.WxUserService;
import com.gongjian.utile.CodeEntity;
import com.gongjian.utile.utile.EasyExcelUtil;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 董浩  导入Excle
 * @Description
 * @create 2019-03-25 20:20
 */
@RestController
public class Excel {

    @Autowired
    WxUserService userService;

    @Autowired
    SqlSession sqlSession;
    /**
     * 导入用户  userTye
     * @param file
     * @return
     */
    @RequestMapping("/excel/user")
    public String user(@RequestParam("file") MultipartFile file,int userType){
        System.out.println(userType);
        if (file.isEmpty()) {
        }
        String fileName = file.getOriginalFilename();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            //文件流
            InputStream inputStream = file.getInputStream();
            List<Object> objectList = EasyExcelUtil.readExcelWithModel(inputStream,User.class,ExcelTypeEnum.XLSX);
            List<User> etuInfoList=(List)objectList;
            for (User u:etuInfoList
                 ) {
                u.setUserIsDelete(0);
                u.setUserType(userType);
                if(userType==0){
                    sqlSession.insert("addStuUser" ,u);
                }else{
                    sqlSession.insert("addTeaUser" ,u);
                }

            }
            sqlSession.commit();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  new CodeEntity(200,"上传成功").toJSONString();
    }

    @RequestMapping("/excel/addUsersClass")
    public String classeUser(@RequestParam("file") MultipartFile file,int classeId){
        System.out.println(classeId);
        if (file.isEmpty()) {
        }
        String fileName = file.getOriginalFilename();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            //文件流
            InputStream inputStream = file.getInputStream();
            List<Object> objectList = EasyExcelUtil.readExcelWithModel(inputStream,User.class,ExcelTypeEnum.XLSX);
            List<User> classes=(List)objectList;
            List<Classe> res=new ArrayList<>();
            for (User user:classes
                 ) {
                User re=  sqlSession.selectOne("findUserByUserNameAndUserNumber",user);
                if(re==null){
                   return new CodeEntity(2000,"上传错误，没有："+user.getUserNumber() +" "+user.getUserName()+"学生的信息").toJSONString();
                }
                Classe classe=new Classe();
                classe.setClasseId(classeId);
                classe.setUserId(re.getUserId());
                res.add(classe);
            }

            for (Classe c :res
                    ) {

                sqlSession.insert("addUsersClass",c);
            }
            sqlSession.commit();


        } catch (IOException e) {
            e.printStackTrace();
        }
        return  new CodeEntity(200,"上传成功").toJSONString();
    }


}
